package org.smart4j.chapter02.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.smart4j.chapter02.helper.DatabaseHelper;
import org.smart4j.chapter02.model.Customer;
import org.smart4j.chapter02.util.PropsUtil;

import java.util.List;
import java.util.Map;
import java.util.Properties;

public class CustomerService {
    private  static  final  Logger LOGGER=LoggerFactory.getLogger(DatabaseHelper.class);
    private  static  final  String DRIVER;
    private static final  String URL;
    private  static final String USERNAME;
    private  static final String PASSWORD;
    static {
        Properties conf= PropsUtil.loadProps("config.properties");
        DRIVER=conf.getProperty("jdbc.driver");
        URL=conf.getProperty("jdbc.url");
        USERNAME=conf.getProperty("jdbc.username");
        PASSWORD=conf.getProperty("jdbc.password");
        try {
            Class.forName(DRIVER);
        }catch (ClassNotFoundException e){

        }
    }
    public List<Customer> getCustomerList(String keyword){
        return null;
    }
    public List<Customer> getCustomerList(){
       // Connection conn=DatabaseHelper.getConnection();
       // List<Customer> customerList=new ArrayList<Customer>();

            String sql="SELECT * FROM customer";
          //  conn= DriverManager.getConnection(URL,USERNAME,PASSWORD);
           // conn=DatabaseHelper.getConnection();
            return  DatabaseHelper.queryEntityList(Customer.class,sql);
          //  PreparedStatement stmt= conn.prepareStatement(sql);
          //  ResultSet rs=stmt.executeQuery();
          //  while (rs.next()){
          //      Customer customer=new Customer();
          //      customer.setId(rs.getLong("id"));
           //     customer.setName(rs.getString("name"));
           //     customer.setContact(rs.getString("contact"));
           //     customer.setEmail(rs.getString("email"));
           //     customer.setRemark(rs.getString("remark"));
          //      customer.setTelephone(rs.getString("telephone"));
           //     customerList.add(customer);
          //  }
    }
    public  Customer getCustomer(long id){
        return null;
    }
    //创建客户
    public  boolean createCustomer(long id,Map<String,Object> fieldMap){
        return DatabaseHelper.insertEntity(Customer.class,fieldMap);
    }
    //更新客户
    public  boolean updateCustomer(long id,Map<String,Object> fieldMap){
        return DatabaseHelper.updateEntity(Customer.class,id,fieldMap);
    }
    //删除客户
    public  boolean deleteCustomer(long id,Map<String,Object> fieldMap){
        return DatabaseHelper.deleteEntity(Customer.class,id);
    }
    public  boolean deleteCustomer(long id){
        return false;
    }

}
